package cn.com.self.apis.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.com.self.apis.collect.entity.CollectionInfo;
import cn.com.self.apis.collect.mapper.CollectionInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import cn.com.self.apis.collect.service.ICollectionInfoService;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import io.swagger.annotations.ApiOperation;
/**
 *
 * @author1 ssh
 * @author2
 * @date 2021-04-28
 */
@Controller
@Api(tags = "个人中心-我的收藏")
public class PersonalMyCollectController {
    @Autowired
    private ICollectionInfoService collectionInfoService;
    @Autowired
    private CollectionInfoMapper collectionInfoMapper;
    /**新增收藏
     * @Description //TODO
     * @return
     **/
    @RequestMapping(value = "/collectionInfo/create", method = RequestMethod.POST)
    @ApiOperation(value = "新增收藏")
    public ResponseEntity create(@RequestBody CollectionInfo entity, HttpServletRequest request){
        entity.setCreateTime(new Date());
        CollectionInfo create = collectionInfoService.create(entity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(create));
    }


    /**分页获取收藏列表
     * @Description //TODO
     * @return
     **/
    @RequestMapping(value = "/collectionInfo/page", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取收藏列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "page", value = "页码", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "size", value = "一页显示条数", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "collectionType", value = "收藏类型（1需求、2用户）", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "memberId", value = "当前登录的会员id", required = true, dataType = "String")
    })
    public ResponseEntity getCollectionInfoPage(HttpServletRequest request, Pageable page){
        //Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        Map<String, Object> queryParams = ParameterUtil.wrapObjectMap(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        return this.collectionInfoService.loadCollectionInfoList(queryParams, page);
    }

    /**获取单个收藏信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "获取单个收藏信息")
    @RequestMapping(value = {"/collectionInfo/load/{id}"}, method = RequestMethod.GET)
    public ResponseEntity loadCollectionInfo(@PathVariable("id") String id, HttpServletRequest request) {
        CollectionInfo collectionInfo = this.collectionInfoService.load(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(collectionInfo)));

    }

    /**修改收藏信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "修改收藏信息")
    @RequestMapping(value = {"/collectionInfo/update/{id}"}, method = RequestMethod.POST)
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody CollectionInfo entity, HttpServletRequest request) {
        CollectionInfo collectionInfo = this.collectionInfoService.load(id);
        CopyUtils.copyProperties(entity, collectionInfo);
        CollectionInfo update = this.collectionInfoService.update(collectionInfo);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(update)));

    }

    /**删除收藏信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "删除收藏信息")
    @RequestMapping(value = "/collectionInfo/delete/{id}",method = RequestMethod.DELETE)
    public ResponseEntity deletes(@PathVariable("id") String id){
        this.collectionInfoService.delete(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }

//    @GetMapping(value = {"/my/collectionInfo/member/id/{userId}"})
//    @ApiOperation(value = "根据用户id查询收藏信息")
//    public ResponseEntity getCollectionInfoByRequirementsId(@PathVariable("userId") String userId, HttpServletRequest request) {
//        Map<String, Object> map = new HashMap(1);
//        map.put("user_id", userId);
//        List<CollectionInfo> collectionInfos = collectionInfoMapper.selectByMap(map);
//        return ResponseEntity.status(HttpStatus.OK).body(new cn.com.self.apis.core.vo.ResultModel<>(collectionInfos));
//    }

}

